package network;
/**
 * @(#)FilesList.java
 * A FilesList object is a list of all the files
 * contained in a parent directory. The constructor
 * of this class has to be used like this :
 * FilesList list = new FilesList ("C:\\Programmes\\Java");
 *
 * @Eric DAVID - Web 45
 * ericm.david@gmail.com
 *
 * Here is the list of the public methods :
 * public	void 		afficheAllFiles ()
 * public	void 		afficheParentDir ()
 * public	String 		getParentPath ()
 * public	long 		getNumberOfFiles ()
 * public	File[] 		getFiles ()
 * public	String[] 	getPaths ()
 * public	String[] 	getNames ()
 *
 * @version 1.00 2007/11/9
 * 
 * Copyright 2008 Eric DAVID
 * 
 * This file is part of Java Saving System.

    Java Saving System is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Java Saving System is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Java Saving System; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.io.File;
import java.util.ArrayList ;

public class FilesList {

/* ===========================================
 * GLOBALS
 * ===========================================*/ 

	// Contains all the listed files
	private File[] ALL_FILES = null;
	
	// Contains all the listed files too
	private ArrayList<File> ARRAY = null;
	
	// This is the parent directory to analyse
	private File PARENT_DIR = null;


/* ===========================================
 * CONSTRUCTORS
 * ===========================================*/ 
    public FilesList(String path) {
    	PARENT_DIR	= new File (path);		// Creates the parent directory File
    	ARRAY 		= new ArrayList<File> (0);	// Creates the ArrayList
    	
    	listTheFiles (PARENT_DIR);			// This gets a list of the files contained in the parent directory
    	convertArray ();					// Converts ArrayList ARRAY to File[] ALL_FILES
    }

/* ===========================================
 * PUBLIC METHODS
 * ===========================================*/ 
    public void afficheAllFiles (){
    	for (int i = 0 ; i < ALL_FILES.length ; i++){
    		System.out.println(ALL_FILES[i].getPath());
    	}
    }
    
    public void afficheParentDir (){
    	System.out.println(PARENT_DIR.getPath());
    }
    
    public String getParentPath (){
    	return PARENT_DIR.getPath();
    }
    
    public long getNumberOfFiles (){
    	return ALL_FILES.length;
    }
    
    public File[] getFiles (){
    	return ALL_FILES;
    }
    
    public String[] getPaths (){
    	int n = ALL_FILES.length;
    	String[] paths = new String [n];
    	for (int i = 0 ; i < n ; i++){
    		paths[i] = ALL_FILES[i].getPath(); 
    	}
    	return paths;
    }
    
    public String[] getNames (){
    	int n = ALL_FILES.length;
    	String[] names = new String [n];
    	for (int i = 0 ; i < n ; i++){
    		names[i] = ALL_FILES[i].getName(); 
    	}
    	return names;
    }

/* ===========================================
 * PRIVATE METHODS
 * ===========================================*/    
    
    /**
	 * Fonction qui Liste des dossier contenant le fichier en parametre
	 * 
	 * @param Fichier
	 */
    
    
    private void listTheFiles (File dir){
    	File[] list = dir.listFiles();	// Makes a list of the dir containing Files
    	for (int i = 0 ; i < list.length ; i++){
    		ARRAY.add(list[i]);    		
    		if (list[i].isDirectory())	listTheFiles(list[i]);    		
    	}    
    }
    
    
    
    private void convertArray(){
    	int n = ARRAY.size();
    	ALL_FILES = new File[n];    	
    	
    	// Transfering the ARRAY elements to the ALL_FILES table of Files
    	for (int i = 0 ; i < n ; i++)	ALL_FILES[i] = (File)ARRAY.get(i);
    }
}